package com.konsung.ksdc.manager.advice;

import android.os.Bundle;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.SpannedString;
import android.text.TextUtils;
import android.text.style.AbsoluteSizeSpan;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.konsung.ksdc.R;
import com.konsung.ksdc.common.network.RequestCallback;
import com.konsung.ksdc.common.network.RequestUtils;
import com.konsung.ksdc.common.network.ResponseBean;
import com.konsung.ksdc.common.utils.Constants;
import com.konsung.ksdc.config.NetConfig;
import com.konsung.ksdc.manager.appmain.BaseActivity;

import butterknife.Bind;
import butterknife.ButterKnife;
import okhttp3.Call;

/**
 * Created by Administrator on 2016/3/18 0018.
 */
public class AddAdviceActivity extends BaseActivity {
    @Bind(R.id.tab_tv_title)
    TextView tvTitle;
    @Bind(R.id.rl_back)
    RelativeLayout rlBack;
    @Bind(R.id.et_suggestion)
    EditText etSuggestion;
    @Bind(R.id.btn_commit)
    Button btnCommit;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_advice_add);

        ButterKnife.bind(this);
        initView();

        btnCommit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                clickCommit();
            }
        });
    }

    @Override
    protected String activityDesc() {
        return getString(R.string.page_add_advice);
    }

    /**
     * 提交
     */
    private void clickCommit() {
        String suggestionContent = etSuggestion.getText().toString().trim();
        if (TextUtils.isEmpty(suggestionContent)) {
            Toast.makeText(this, R.string.tip_advice_empty, Toast
                    .LENGTH_SHORT).show();
            return;
        }

        if (!RequestUtils.checkNetwork(AddAdviceActivity.this)) {
            return;
        }

        RequestUtils.commitSuggestion(NetConfig.currentEmpid,
                suggestionContent, new RequestCallback() {
                    @Override
                    public void onError(Call call, Exception e) {
                    }

                    @Override
                    public void onResponse(ResponseBean responseBean) {
                        String resultCode = responseBean.getResultCode();
                        if (resultCode.equals(Constants.REQUEST_OK)) {
                            Toast.makeText(AddAdviceActivity.this,
                                    R.string.advice_commited,
                                    Toast.LENGTH_SHORT).show();
                            finish();
                        }
                    }
                });
    }

    /**
     * 初始化
     */
    private void initView() {
        tvTitle.setText(R.string.title_advice_add);
        rlBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });

        // ### 自定义hint大小 ###
        SpannableString ss = new SpannableString(getString(R.string
                .et_advice_hint));
        // 新建一个属性对象,设置文字的大小
        AbsoluteSizeSpan ass = new AbsoluteSizeSpan(18, true); // 附加属性到文本
        // 第二个属性是否为dp
        ss.setSpan(ass, 0, ss.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);

        // 设置hint 一定要进行转换,否则属性会消失
        etSuggestion.setHint(new SpannedString(ss));
    }
}
